package com.example.androidlearning2422.ui.act
import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView

class WechatActivity : Activity() {

    // 消息数据（保持不变）
    private val names = arrayOf(
        "老史家",
        "霸州-史倩倩",
        "金甲工坊（名人堂）",
        "金甲工坊（大家庭）",
        "课程建设采购需求对接",
        "祝文飞",
        "老祝家",
        "三届国赛移动项目冲刺",
        "人社三届国赛-移动应用开发",
        "津电-火星产教融合"
    )
    private val lastMessages = arrayOf(
        "\"霸州-史倩倩\"撤回了一条消息",
        "[语音] 3\"",
        "工坊-曹元顺: 嗯嗯，那就按现在这个吧，不改了",
        "https://m.alltuu.com/album/4026674759/e0c93c...",
        "9527: [文件] 1022v2: 2025年课程建设项目...",
        "[图片]",
        "[视频号] 小鱼打造真实感IP的视频",
        "电子-吴涛: 各位老师，把这些信息发我一下",
        "罗瑜 青海代表队: #接龙 已经填写的老师请接龙...",
        "是得抓紧，我尽量写出亮点来，😃"
    )
    private val times = arrayOf(
        "昨天",
        "昨天",
        "周一",
        "10月25日",
        "10月22日",
        "10月21日",
        "10月20日",
        "10月13日",
        "9月25日",
        "3月18日"
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createWeChatLayout())
    }

    private fun createWeChatLayout(): View {
        // 根布局
        val rootLayout = LinearLayout(this).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            orientation = LinearLayout.VERTICAL
            setBackgroundColor(Color.parseColor("#F5F5F5"))
        }

        // 1. 顶部标题栏（标题改为“消息”）
        val titleBar = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dp2px(48)
            )
            setBackgroundColor(Color.parseColor("#EDEDED"))
            gravity = Gravity.CENTER_VERTICAL
            setPadding(dp2px(16), 0, dp2px(16), 0)
        }

        val titleText = TextView(this).apply {
            text = "消息(1)" // 标题从“微信”改为“消息”
            textSize = 18f
            setTextColor(Color.BLACK)
            typeface = android.graphics.Typeface.DEFAULT_BOLD
            layoutParams = LinearLayout.LayoutParams(
                0,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                1f
            )
            gravity = Gravity.CENTER
        }
        titleBar.addView(titleText)

        // 搜索按钮（保持不变）
        val searchBtn = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(dp2px(24), dp2px(24)).apply {
                marginEnd = dp2px(16)
            }
            setImageResource(android.R.drawable.ic_menu_search)
            scaleType = ImageView.ScaleType.CENTER
        }
        titleBar.addView(searchBtn)

        // 加号按钮（保持不变）
        val addBtn = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(dp2px(24), dp2px(24))
            setImageResource(android.R.drawable.ic_menu_add)
            scaleType = ImageView.ScaleType.CENTER
        }
        titleBar.addView(addBtn)

        rootLayout.addView(titleBar)

        // 2. 消息列表区域（保持不变）
        val scrollView = ScrollView(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                0,
                1f
            )
        }

        val messageList = LinearLayout(this).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            orientation = LinearLayout.VERTICAL
        }

        for (i in names.indices) {
            messageList.addView(
                createMessageItem(
                    names[i],
                    lastMessages[i],
                    times[i]
                )
            )
            val divider = View(this@WechatActivity).apply {
                layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    1
                ).apply {
                    marginStart = dp2px(72)
                }
                setBackgroundColor(Color.parseColor("#E5E5E5"))
            }
            messageList.addView(divider)
        }

        scrollView.addView(messageList)
        rootLayout.addView(scrollView)

        // 3. 底部导航栏（重点修改：图标和文字）
        val bottomNav = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dp2px(56)
            )
            orientation = LinearLayout.HORIZONTAL
            setBackgroundColor(Color.parseColor("#E5E5E5")) // 顶部边框色
        }

        val navContent = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            orientation = LinearLayout.HORIZONTAL
            setBackgroundColor(Color.WHITE)
            setPadding(0, 1, 0, 0)
        }
        bottomNav.addView(navContent)

        // 底部导航项修改：
        // 1. 消息（原微信项，替换为消息图标）
        navContent.addView(
            createNavItem(
                title = "消息",
                iconRes = android.R.drawable.ic_dialog_email, // 系统消息图标（信封样式）
                isSelected = true
            )
        )
        // 2. 通讯录（保持不变）
        navContent.addView(
            createNavItem(
                title = "通讯录",
                iconRes = android.R.drawable.ic_menu_recent_history,
                isSelected = false
            )
        )
        // 3. 发现（保持不变）
        navContent.addView(
            createNavItem(
                title = "发现",
                iconRes = android.R.drawable.ic_menu_compass,
                isSelected = false
            )
        )
        // 4. 我（替换为人形图标）
        navContent.addView(
            createNavItem(
                title = "我",
                iconRes = android.R.drawable.ic_menu_myplaces, // 系统人形图标（用户样式）
                isSelected = false
            )
        )

        rootLayout.addView(bottomNav)

        return rootLayout
    }

    // 构建单个消息项（保持不变，粉色头像）
    private fun createMessageItem(name: String, lastMsg: String, time: String): View {
        val itemLayout = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                dp2px(72)
            )
            setPadding(dp2px(16), 0, dp2px(16), 0)
            gravity = Gravity.CENTER_VERTICAL
            setBackgroundColor(Color.WHITE)
        }

        // 粉色圆形头像
        val avatar = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(dp2px(56), dp2px(56)).apply {
                marginEnd = dp2px(16)
            }
            val avatarBg = GradientDrawable().apply {
                shape = GradientDrawable.OVAL
                setColor(Color.parseColor("#FFC0CB")) // 粉色
            }
            background = avatarBg
            scaleType = ImageView.ScaleType.CENTER_CROP
        }
        itemLayout.addView(avatar)

        // 中间内容区（保持不变）
        val contentLayout = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                0,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                1f
            )
            orientation = LinearLayout.VERTICAL
            setPadding(0, dp2px(10), 0, dp2px(10))
        }

        val nameTv = TextView(this).apply {
            text = name
            textSize = 17f
            setTextColor(Color.BLACK)
            typeface = android.graphics.Typeface.DEFAULT_BOLD
        }
        contentLayout.addView(nameTv)

        val msgTv = TextView(this).apply {
            text = lastMsg
            textSize = 14f
            setTextColor(Color.parseColor("#8A8A8A"))
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = dp2px(4)
            }
        }
        contentLayout.addView(msgTv)
        itemLayout.addView(contentLayout)

        val timeTv = TextView(this).apply {
            text = time
            textSize = 12f
            setTextColor(Color.parseColor("#8A8A8A"))
        }
        itemLayout.addView(timeTv)

        return itemLayout
    }

    // 构建底部导航项（保持样式逻辑）
    private fun createNavItem(
        title: String,
        iconRes: Int,
        isSelected: Boolean
    ): View {
        val navItem = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                0,
                ViewGroup.LayoutParams.MATCH_PARENT,
                1f
            )
            orientation = LinearLayout.VERTICAL
            gravity = Gravity.CENTER
            setPadding(0, dp2px(8), 0, dp2px(4))
        }

        val icon = ImageView(this).apply {
            layoutParams = LinearLayout.LayoutParams(dp2px(24), dp2px(24))
            setImageResource(iconRes)
            setColorFilter(if (isSelected) Color.parseColor("#07C160") else Color.parseColor("#8A8A8A"))
            scaleType = ImageView.ScaleType.CENTER
        }
        navItem.addView(icon)

        val titleTv = TextView(this).apply {
            text = title
            textSize = 10f
            setTextColor(if (isSelected) Color.parseColor("#07C160") else Color.parseColor("#8A8A8A"))
            layoutParams = LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                topMargin = dp2px(2)
            }
        }
        navItem.addView(titleTv)

        return navItem
    }

    // dp转px工具
    private fun dp2px(dp: Int): Int {
        val density = resources.displayMetrics.density
        return (dp * density + 0.5f).toInt()
    }
}
